import { Entity, PrimaryGeneratedColumn, Column,  } from "typeorm";
import { ManualTimestamp } from "../../../decorators/manual-timestamp.decorator";
import { GameType } from "./index";

@Entity("casino_rankings")
@ManualTimestamp()
export class CasinoRanking {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ comment: "用户ID" })
  userId!: number;

  @Column({ type: "varchar", length: 64, comment: "用户名称" })
  userName!: string;

  @Column({ type: "tinyint", comment: "游戏类型：1-大小，2-赤壁，3-猜拳" })
  gameType!: GameType;

  @Column({ type: "bigint", default: 0, comment: "总获胜金额" })
  totalWin!: number;

  @Column({ type: "int", default: 0, comment: "获胜次数" })
  winCount!: number;

  @Column({ type: "bigint", default: 0, comment: "总下注金额" })
  totalBet!: number;

  @Column({ type: "bigint", default: 0, comment: "排行分数" })
  rankScore!: number;

  @Column({ type: "varchar", length: 32, comment: "称号", nullable: true })
  title?: string;

  @Column("datetime", { name: "updated_at", nullable: true, comment: "更新时间" })
  updated_at!: Date | null;
// 计算属性
  get winRate(): number {
    if (this.totalBet === 0) return 0;
    return (this.totalWin / this.totalBet) * 100;
  }

  get netProfit(): number {
    return this.totalWin - this.totalBet;
  }

  get gameTypeName(): string {
    const names = {
      [GameType.DICE]: "大小玩法",
      [GameType.CHIBI]: "赤壁玩法",
      [GameType.ROCK_PAPER_SCISSORS]: "美人计玩法",
    };
    return names[this.gameType] || "未知";
  }

  get titleLevel(): string {
    if (this.rankScore >= 1000000) return "赌神";
    if (this.rankScore >= 500000) return "赌圣";
    if (this.rankScore >= 100000) return "赌侠";
    if (this.rankScore >= 50000) return "赌狂";
    if (this.rankScore >= 10000) return "赌魔";
    if (this.rankScore >= 5000) return "赌痴";
    if (this.rankScore >= 1000) return "赌怪";
    if (this.rankScore >= 500) return "赌鬼";
    if (this.rankScore >= 100) return "赌徒";
    return "新手";
  }

  // 更新排行分数
  updateRankScore(): void {
    // 排行分数 = 总获胜金额 * 0.7 + 获胜次数 * 1000 + 净盈利 * 0.3
    const netProfit = this.netProfit;
    this.rankScore = Math.floor(
      this.totalWin * 0.7 + this.winCount * 1000 + Math.max(0, netProfit) * 0.3
    );
  }

  // 添加获胜记录
  addWin(winAmount: number): void {
    this.totalWin += winAmount;
    this.winCount += 1;
    this.updateRankScore();
  }

  // 添加下注记录
  addBet(betAmount: number): void {
    this.totalBet += betAmount;
    this.updateRankScore();
  }

  // 重置统计数据
  resetStats(): void {
    this.totalWin = 0;
    this.winCount = 0;
    this.totalBet = 0;
    this.rankScore = 0;
    this.title = undefined;
  }
}
